//获取系统时间
var newDate = '';
getLangDate();
//值小于10时，在前面补0
function dateFilter(date){
    if(date < 10){return "0"+date;}
    return date;
}
function getLangDate(){
    var dateObj = new Date(); //表示当前系统时间的Date对象
    var year = dateObj.getFullYear(); //当前系统时间的完整年份值
    var month = dateObj.getMonth()+1; //当前系统时间的月份值
    var date = dateObj.getDate(); //当前系统时间的月份中的日
    var day = dateObj.getDay(); //当前系统时间中的星期值
    var weeks = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    var week = weeks[day]; //根据星期值，从数组中获取对应的星期字符串
    var hour = dateObj.getHours(); //当前系统时间的小时值
    var minute = dateObj.getMinutes(); //当前系统时间的分钟值
    var second = dateObj.getSeconds(); //当前系统时间的秒钟值
    var timeValue = "" +((hour >= 12) ? (hour >= 18) ? "晚上" : "下午" : "上午" ); //当前时间属于上午、晚上还是下午
    newDate = dateFilter(year)+"年"+dateFilter(month)+"月"+dateFilter(date)+"日 "+" "+dateFilter(hour)+":"+dateFilter(minute)+":"+dateFilter(second);
    document.getElementById("nowTime").innerHTML = "亲爱的用户，" + timeValue +"好！ 欢迎使用 BestTrader交易结算系统。当前时间为： "+newDate+"　"+week;
    setTimeout("getLangDate()",1000);
}

function initLayDate(laydate) {
    var dateObj = new Date(); //表示当前系统时间的Date对象
    var year = dateObj.getFullYear(); //当前系统时间的完整年份值
    var month = dateObj.getMonth() + 1; //当前系统时间的月份值
    var date = dateObj.getDate(); //当前系统时间的月份中的日
    var preDate = dateObj.getDate() - 1; 
    var preYear = dateObj.getMonth() == 0 ? (dateObj.getFullYear() - 1) : dateObj.getFullYear(); //当前系统时间的月份值
    var preMonth = dateObj.getMonth() == 0 ? 11 : dateObj.getMonth(); //当前系统时间的月份值
    preDate =  preDate <= 0 ? 01 : preDate;
    //var initDate = preYear + '-' + preMonth + '-' + date + ' - ' + year + '-' + month + '-' + date;
    var initDate1 = preYear + '-' + month + '-' + preDate;
    var initDate2 = year + '-' + month + '-' + date;

    laydate.render({
        elem: '#searchStart', //指定元素
        //value: '2019-02-01 - 2019-03-01',
        value: initDate1
        //range: true
    });
    laydate.render({
        elem: '#searchEnd', //指定元素
        value: initDate2
    });
}

function initChart(titles, data) {
   
    var option = {
        title: {
            text: '名下交易总手数',
            //subtext: '纯属虚构',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 10,
            top: 40,
            bottom: 20,
            //data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
            data: titles
        },
        toolbox: {
            show: true,
            feature: {
                mark: { show: true },
                dataView: { show: true, readOnly: false },
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 1548
                        }
                    }
                },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        //calculable: true,
        series: [
            {
                name: '交易手数',
                type: 'pie',
                radius: '75%',
                center: ['50%', '60%'],
                data: data
                //data: [
                //    { value: 335, name: '直接访问' },
                //    { value: 310, name: '邮件营销' },
                //    { value: 234, name: '联盟广告' },
                //    { value: 135, name: '视频广告' },
                //    { value: 1548, name: '搜索引擎' }
                //]
            }
        ]
    };
    var myChart = echarts.init(document.getElementById('myChart'));
    myChart.setOption(option);
}

function more(obj) {
    //$(obj).html('');
    obj.innerHTML = '';
    if (obj.parentNode && obj.parentNode.parentNode) {
        var child = obj.parentNode.parentNode.children;
        console.log(child);
        for (var i = 0; i < child.length; i++) {
            var item = child[i];
            item.style.display = 'inherit';
        }
    }
    
}

layui.use(['form', 'element', 'layer', 'jquery', 'laydate'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laydate = layui.laydate,
        element = layui.element,
        $ = layui.jquery;

    // 设置直客数据显示
    var userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'));
    userInfo && (userInfo.roleId != 6) && $("#agentPanel").show();

    initLayDate(laydate);

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        if ($(".searchAgent").val() !== '' || $(".searchStart").val() !== '' || $(".searchEnd").val() !== '') {
            $.ajax({
                type: 'GET',
                url: '/Home/DataStatistics/',
                data: {
                    ManagerId: $(".searchAgent").val(),
                    StartDate: $(".searchStart").val(),
                    EndDate: $(".searchEnd").val(),
                    SearchDateType: $("input[name='searchDateType']:checked").val()

                },
                dataType: "json",
                success: function (res) {//res为相应体,function为回调函数
                    if (res.Success) {
                        var data = res.Data;
                        var order = data.DealedOrder;
                        var rebate = data.Rebate;
                        if (order) {
                            initChart(order.ChartTitle, order.ChartData);
                            $("#searchTotal").text(order.Total + "手");
                        }
                        if (rebate) {
                            $("#searchRebateTotal").text(rebate.Total + "元");
                            $("#searchRebateSelfTotal").text(rebate.SelfTotal + "元");
                            $("#searchRebateAgentTotal").text(rebate.AgentTotal + "元");
                            $("#searchTerminalNum").text(rebate.TerminalNum + "人");
                            $("#searchCostCoin").text(rebate.CostCoin + "元");
                            
                            var TerminalInfo = '<li><h3 class="layui-inline">暂无数据</h3></li>';
                            var RebateData = '<li><h3 class="layui-inline">暂无数据</h3></li>';
                            if (rebate.TerminalInfo) {
                                var len1 = rebate.TerminalInfo.length;
                                len1 && (TerminalInfo = '');
                                for (var i = 0; i < len1; i++) {
                                    var item = rebate.TerminalInfo[i];
                                    if (i < 4) {
                                        TerminalInfo += '<li><h3 class="layui-inline">' + item + '</h3></li>';
                                    } else if (i == 4) {
                                        TerminalInfo += '<li><h3 class="layui-inline">' + item + '</h3><span style="display:inline-block; text-indent:2em;" onclick="more(this)">更多...</span></li>';
                                    } else {
                                        TerminalInfo += '<li style="display:none;"><h3 class="layui-inline">' + item + '</h3></li>';
                                    }
                                }
                            }
                            if (rebate.RebateData) {
                                var len2 = rebate.RebateData.length;
                                len2 && (RebateData = '');
                                for (var i = 0; i < len2; i++) {
                                    var item = rebate.RebateData[i];
                                    if (i < 4) {
                                        RebateData += '<li><h3 class="layui-inline">' + item.Title + '</h3>&nbsp;&nbsp;<span class="layui-badge-rim">' + item.Date + '</span></li>';
                                    } else if (i == 4) {
                                        RebateData += '<li><h3 class="layui-inline">' + item.Title + '</h3>&nbsp;<span class="layui-badge-rim">' + item.Date + '</span><span style="display:inline-block; text-indent:2em;" onclick="more(this)">更多...</span></li>';
                                    } else {
                                        RebateData += '<li style="display:none;"><h3 class="layui-inline">' + item.Title + '</h3>&nbsp;&nbsp;<span class="layui-badge-rim">' + item.Date + '</span></li>';
                                    }
                                }
                            }

                            $("#searchTerminal").html(TerminalInfo);
                            $("#searchRebates").html(RebateData);
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
                }
            });
        } else {
            layer.msg("请输入搜索的内容");
        }
    });

    //上次登录时间【此处应该从接口获取，实际使用中请自行更换】
    $(".loginTime").html(newDate.split("日")[0] + "日</br>" + newDate.split("日")[1]);
    //icon动画
    $(".panel a").hover(function () {
        $(this).find(".layui-anim").addClass("layui-anim-scaleSpring");
    }, function () {
        $(this).find(".layui-anim").removeClass("layui-anim-scaleSpring");
    });
    $(".panel a").click(function () {
        parent.addTab($(this));
    });
    //系统基本参数
    if (window.sessionStorage.getItem("systemParameter")) {
        var systemParameter = JSON.parse(window.sessionStorage.getItem("systemParameter"));
        fillParameter(systemParameter);
    } else {
        $.ajax({
            url: "/json/systemParameter.json",
            type: "get",
            dataType: "json",
            success: function (data) {
                fillParameter(data);
            }
        });
    }
    //填充数据方法
    function fillParameter(data) {
        //判断字段数据是否存在
        function nullData(data) {
            if (data === '' || data === "undefined") {
                return "未定义";
            } else {
                return data;
            }
        }
        $(".version").text(nullData(data.version));      //当前版本
        $(".author").text(nullData(data.author));        //开发作者
        $(".homePage").text(nullData(data.homePage));    //网站首页
        $(".server").text(nullData(data.server));        //服务器环境
        $(".dataBase").text(nullData(data.dataBase));    //数据库版本
        $(".maxUpload").text(nullData(data.maxUpload));    //最大上传限制
        $(".userRights").text(nullData(data.userRights));//当前用户权限
    }

    //最新文章列表
    $.get("/json/newsList.json", function (data) {
        var hotNewsHtml = '';
        for (var i = 0; i < 5; i++) {
            hotNewsHtml += '<tr>'
                + '<td align="left"><a href="javascript:;"> ' + data.data[i].newsName + '</a></td>'
                + '<td>' + data.data[i].newsTime.substring(0, 10) + '</td>'
                + '</tr>';
        }
        $(".hot_news").html(hotNewsHtml);
        $(".userAll span").text(data.length);
    });

    //用户数量
    $.get("/json/userList.json", function (data) {
        $(".userAll span").text(data.count);
    });

    //外部图标
    $.get(iconUrl, function (data) {
        $(".outIcons span").text(data.split(".icon-").length - 1);
    });

});
